#!/bin/sh
# 安装时，需要在install_mysql8.sh的同一目录下也上传mslib.s和my.cnf这两个文件
# 安装示例: install_mysql8.sh install
# 安装时必须使用root用户
# 如果要改变安装目录和数据存储目录，需要修改install_mysql8.sh和my.cnf两个文件
# 安装目录规范：
    ## 安装目录：/data/tools/mysql
    ## 数据目录：/data/tools/mysql/data
    ## 日志目录：/data/tools/mysql/logs
    ## 配置文件目录：/etc/my.cnf

    
CURR_DIR=$(cd "$(dirname $0)"; pwd)

source $CURR_DIR/mslib.s

if [ $# -lt 1 ]
then
    LogErrorMsg "The install script needs a parameter: install or uninstall."
    exit 1
fi

INSTALL_TYPE=$1

if [ "${INSTALL_TYPE}" != "install" ] && [ "${INSTALL_TYPE}" != "uninstall" ]
then
    LogWarnMsg "Database package doesn't support the operation: ${INSTALL_TYPE}."
    exit 1
fi

#判断脚本用户
CURR_USER=`whoami | awk '{print $1}'`
if [ $CURR_USER != "root" ]
then
    LogWarnMsg "the current logined user is not root! please install with root"
    exit 1
fi

MODULE_NAME=mysql8024
INSTALL_DATE=$(date +%F' '%T)
mysql_home=/data/tools/mysql
basedir=$mysql_home
datadir=$mysql_home/data
logdir=$mysql_home/logs
pidfile=$datadir/`hostname`.pid
user=mysql
group=mysql
# 下载的mysql文件名
TAR_UNPACK_DIR=mysql-8.0.24-linux-glibc2.12-x86_64 


#卸载mysql
uninstall() {
    #如果有mysql服务就停掉
   # COUNT=`ps -ef | grep -v grep | grep -c mysqld`
   # if [ ${COUNT} -ne 0 ]; then
    if pgrep mysqld & >/dev/null
    then 
        /etc/init.d/mysqld stop
    fi

    rm -rf $mysql_home
    rm -f /etc/profile.d/mysql.sh
    rm -f /etc/my.cnf
    rm -f /etc/init.d/mysqld
    userdel $user
    groupdel $group
}

#安装mysql
install() {
    LogMsg "${INSTALL_DATE} install $MODULE_NAME..."
    #卸载mysql
    uninstall
    #创建mysql用户
    groupadd $group
    useradd -r -g $group -s /bin/false $user

    #检查db路径
    if [ ! -d $mysql_home ]; then
        mkdir -p $mysql_home
    else
        rm -rf $mysql_home/*
    fi
    mkdir -p $basedir/mysql-files
    mkdir -p $datadir
    mkdir -p $logdir
    touch $logdir/error.log

    mkdir -p /data/tools
    cd /data/tools
    echo 'downloading...'
    wget -c  https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-8.0/$TAR_UNPACK_DIR.tar.xz || exit 1

    tar -xvf $TAR_UNPACK_DIR.tar.xz -C $mysql_home
    mv $mysql_home/$TAR_UNPACK_DIR/* $basedir
    rmdir $mysql_home/$TAR_UNPACK_DIR

    chown -R $user:$group $mysql_home

    $basedir/bin/mysqld  --user=$user --datadir=$datadir --basedir=$basedir --lower-case-table-names=1 --default-authentication-plugin=mysql_native_password 
    $basedir/bin/mysql_ssl_rsa_setup -d $datadir

    cp $basedir/support-files/mysql.server /etc/init.d/mysqld
    sed -i "s#^basedir=#basedir=$basedir#;s#^datadir=#datadir=$datadir#" /etc/init.d/mysqld
    
    chmod 700 /etc/init.d/mysqld
    chkconfig --add mysqld
    chkconfig --level 345 mysqld on

    cp $CURR_DIR/my.cnf /etc/

    echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
    source /etc/profile

    #启动mysql
    /etc/init.d/mysqld start

    if [ -f $pidfile ]; then
        LogSucMsg "${INSTALL_DATE} install ${MODULE_NAME} successfully."
        #\cp -f $CURR_DIR/release-note $mysql_home
        return 0
    else
        LogErrorMsg "${INSTALL_DATE} install ${MODULE_NAME} failed."
        return 1
    fi
}

#执行判断，如果加上uninstall参数就卸载
if [ "${INSTALL_TYPE}" == "uninstall" ]
then
    LogMsg "${INSTALL_DATE} uninstall $MODULE_NAME..."
    uninstall
    LogSucMsg "${INSTALL_DATE} uninstall ${MODULE_NAME} successfully."
else
    install
    if [ "$?" == "1" ]
    then
        exit 1
    fi
fi

exit 0
